package com.bocai.kelalacar.ui.car.order.act;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.kelalacar.App;
import com.bocai.kelalacar.R;
import com.bocai.kelalacar.base.BaseActivity;
import com.bocai.kelalacar.bean.CarDriverBean;
import com.bocai.kelalacar.bean.MyGradOrderBean;
import com.bocai.kelalacar.bean.MyPublishStrokeBean;
import com.bocai.kelalacar.bean.OrderDetailBean;
import com.bocai.kelalacar.ui.car.main.act.MainActivity;
import com.bocai.kelalacar.ui.car.order.contract.MyOrderContract;
import com.bocai.kelalacar.ui.car.order.module.MyOrderModule;
import com.bocai.kelalacar.ui.car.order.presenter.MyOrderPresenter;
import com.bocai.kelalacar.util.DateUtil;
import com.bocai.kelalacar.widget.SingleAlertDialog;

import java.util.Date;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by yuanfei on 2017/6/21.
 */

public class MyEmptyInformationDetailAct extends BaseActivity implements MyOrderContract.view{
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.right_btn)
    Button rightBtn;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.ll1)
    LinearLayout ll1;
    @BindView(R.id.tv_order_information)
    TextView tvOrderInformation;
    @BindView(R.id.tv_car_information)
    TextView tvCarInformation;
    @BindView(R.id.tv_travel_name)
    TextView tvTravelName;
    @BindView(R.id.tv_travel_phone)
    TextView tvTravelPhone;
    @BindView(R.id.tv_travel_data)
    TextView tvTravelData;
    @BindView(R.id.ll_you_information)
    LinearLayout llYouInformation;
    @BindView(R.id.ll_travel)
    LinearLayout llTravel;
    @BindView(R.id.tv_start_address)
    TextView tvStartAddress;
    @BindView(R.id.tv_end_address)
    TextView tvEndAddress;
    @BindView(R.id.tv_start_data)
    TextView tvStartData;
    @BindView(R.id.tv_end_data)
    TextView tvEndData;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.tv_start_car)
    TextView tvStartCar;
    @BindView(R.id.tv_driver_name)
    TextView tvDriverName;
    @BindView(R.id.ll_car)
    LinearLayout llCar;
    @BindView(R.id.btn)
    Button btn;
    @BindView(R.id.ll3)
    LinearLayout ll3;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @Inject
    MyOrderPresenter presenter;
    String orderId;
    private int index = 0;
    private OrderDetailBean bean;
    @BindView(R.id.ll_back)
    LinearLayout llBack;

    @Override
    public int getLayoutId() {
        return R.layout.act_my_empty_infor_detail;
    }

    @Override
    public void initPresenter() {
        App.get(this).getAppComponent().addSub(new MyOrderModule(this)).inject(this);
    }

    @Override
    public void initView() {
        initToolbar("我的空车信息");
        initBack(imgBack,llBack);
        orderId = getIntent().getStringExtra("orderId");
        presenter.orderDetail(orderId);
    }

    @Override
    public void myStrokePublish(MyPublishStrokeBean bean) {
        App.get(this).getAppComponent().addSub(new MyOrderModule(this)).inject(this);
    }

    @Override
    public void myGradOrder(MyGradOrderBean bean) {

    }

    @OnClick({R.id.btn})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.btn:
                if (index == 0){
                    setDialog();
                }else if (index == 2){
                    if (TextUtils.isEmpty(bean.getStartTime())){
                        presenter.startStroke(orderId);
                    }else {
                        presenter.finishOrder(orderId);
                    }

                }
                break;
        }
    }


    @Override
    public void getOrderDetail(OrderDetailBean bean) {
        this.bean = bean;
        tvStartAddress.setText(bean.getOriginCity());
        tvEndAddress.setText(bean.getArriveCity());
        tvStartData.setText(bean.getBeginTime());
        tvEndData.setText(bean.getEndTime());
        tvNum.setText(bean.getApplySeats()+"人");
        tvStartCar.setText(bean.getCarLicense());
        tvDriverName.setText(bean.getDriver()+" "+bean.getDriverPhone());
        if (bean.getOrderStatus() == 0){
            index = 0;
            ll1.setVisibility(View.GONE);
            llTravel.setVisibility(View.GONE);
            ll3.setVisibility(View.VISIBLE);
            llCar.setVisibility(View.VISIBLE);
            btn.setText("取消");

        }else if (bean.getOrderStatus() == 1){
            index = 1;
            ll1.setVisibility(View.VISIBLE);
            llTravel.setVisibility(View.VISIBLE);
            ll3.setVisibility(View.GONE);
            llCar.setVisibility(View.VISIBLE);
            tvContent.setText("等待旅行社付款");

            if (!TextUtils.isEmpty(bean.getOrderMemberPhone())) {
                String bb = bean.getOrderMemberPhone().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1********");
                tvTravelPhone.setText(bb);
            }
            if (!TextUtils.isEmpty(bean.getOrderMemberName())) {
                String aa = bean.getOrderMemberName().substring(0, 1);
                tvTravelName.setText(aa + "**");
            }

//            tvTravelName.setText(bean.getOrderMemberName());
//            tvTravelPhone.setText(bean.getOrderMemberPhone());
            tvTravelData.setText(bean.getAppointTime());
            btn.setVisibility(View.GONE);

        }else if (bean.getOrderStatus() == 2||bean.getOrderStatus() == 5){
            index = 2;
            ll1.setVisibility(View.VISIBLE);
            llTravel.setVisibility(View.VISIBLE);
            ll3.setVisibility(View.GONE);
            llCar.setVisibility(View.VISIBLE);
            tvTravelName.setText(bean.getOrderMemberName());
            tvTravelPhone.setText(bean.getOrderMemberPhone());
            tvTravelData.setText(bean.getAppointTime());
            btn.setVisibility(View.VISIBLE);
            btn.setText("开始行程");
            if (!TextUtils.isEmpty(bean.getStartTime())){
                btn.setEnabled(false);
                countDown(bean.getStartTime());

            }
        }else if (bean.getOrderStatus() == 3){
            index = 3;
            ll1.setVisibility(View.VISIBLE);
            llTravel.setVisibility(View.VISIBLE);
            ll3.setVisibility(View.GONE);
            llCar.setVisibility(View.VISIBLE);
            tvTravelName.setText(bean.getOrderMemberName());
            tvTravelPhone.setText(bean.getOrderMemberPhone());
            tvTravelData.setText(bean.getAppointTime());
            btn.setVisibility(View.GONE);
            tvContent.setText("订单已于"+bean.getFinishTime()+"结束");
        }else if (bean.getOrderStatus() == 7){
            index = 1;
            ll1.setVisibility(View.VISIBLE);
            llTravel.setVisibility(View.VISIBLE);
            ll3.setVisibility(View.GONE);
            llCar.setVisibility(View.VISIBLE);
            tvContent.setText("等待旅行社付款");
            tvTravelName.setText(bean.getOrderMemberName());
            tvTravelPhone.setText(bean.getOrderMemberPhone());
            tvTravelData.setText(bean.getAppointTime());
            btn.setVisibility(View.GONE);
        }
    }

    @Override
    public void getCarDriverList(CarDriverBean bean) {

    }

    @Override
    public void teamArrangeSuccess() {

    }

    @Override
    public void startStrokeSuccess() {
        presenter.orderDetail(orderId);
    }

    @Override
    public void cancelOrderSuccess() {
        showShortToast("取消成功");
        finish();
    }

    @Override
    public void finishOrderSuccess() {
        showErrorTip("完成订单");
        presenter.orderDetail(orderId);

    }

    @Override
    public void showLoading(String title) {
        startProgressDialog(title);
    }

    @Override
    public void stopLoading() {
        stopProgressDialog();
    }

    @Override
    public void showErrorTip(String msg) {
        showShortToast(msg);
        if (msg.equals("您的登录已失效，请重新登录")){
            startActivity(new Intent(this,MainActivity.class).putExtra("index",1));
            finish();
        }
    }

    public void setDialog(){
        new SingleAlertDialog(mContext).builder().setTitle("确定取消？")
                .setPositiveButton("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        presenter.cancelOrder(orderId);
                    }
                }).setNegativeButton("点错了", new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        }).show();
    }
    public void countDown(final String time) {

        Date startDate = DateUtil.setStringToData(time);
        Date date = new Date();
        long between = 3*60*60*1000 - (date.getTime() - startDate.getTime());

        if (between>0) {

            CountDownTimer countDownTimer = new CountDownTimer(between, 1) {
                @Override
                public void onTick(long millisUntilFinished) {
                    btn.setText(DateUtil.formatTime3(millisUntilFinished) + " 后可以结束行程");
                }

                @Override
                public void onFinish() {
                    btn.setEnabled(true);
                    btn.setText("完成行程");
                }
            };
            countDownTimer.start();
        }else {
            btn.setEnabled(true);
            btn.setText("完成行程");
        }
    }
}
